#!/bin/sh

sleep_count=1

# for debug: settings put global perf_pid $(pidof com.qiyi.video.pad)
# com.ss.android.lark
# com.android.gallery3


#echo "params size=$#"

dump_app(){
    pkg=$1
    pid=$(pidof $pkg)
    if [ -z "$pid" ]; then
        echo "app not start"
        return
    else
        echo "#### dump app:$pkg, pid=$pid."
    fi
    top_out=`top -b -n 1 -o %CPU -o %MEM -p $pid`
    s1=${top_out##*MEM}
    s2=${top_out##*sys }
    ar=(${s1// / })
    #echo "### 1 after cut, s1=$s1, s2=$s2"
    cpu=${ar[0]}
    mem=${ar[1]}
    idle=${s2%%idle*}
    echo "        $pkg : cpu=$cpu, mem=$mem, idle=$idle."
    settings put global perf_cpu_$pid $cpu
    settings put global perf_mem_$pid $mem
    settings put global perf_idle $idle
    #echo "#### exec settings get perf_cpu and perf_idle_cpu"
    #settings get global perf_cpu_$pid
    #settings get global perf_mem_$pid
    #settings get global perf_idle
    #echo "###################################"
}

test(){
}
############ start #####################
echo "pkg size=$#"
# TODO perf tool获取su权限或是root用户组
pid_of_ss=$(pidof system_server)
settings put global pid_of_ss $pid_of_ss
# echo "######### pid_of_ss = $pid_of_ss"
if [ $# -ge 1 ]; then
    settings put global perf_apps "$*"
    echo "get global perf_apps:"
    settings get global perf_apps
    while [ 1 ];do
        echo "loop once"
        for i in $*; do
            echo ">>dump:$i<<"
            dump_app $i
        done
        echo ""
        echo ""
        sleep $sleep_count
    done
else
    "must give package name list, exit."
fi
